package cn.flkj.lucia2a2.controllers;

import cn.flkj.lucia2a2.config.EnvRunner;
import cn.flkj.lucia2a2.config.Org2Runner;
import cn.flkj.lucia2a2.intermediate.FabricManagerMost;
import cn.flkj.lucia2a2.intermediate.IntermediateOrg;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

/**
 * @author : 州长在手 2021/1/11 上午11:33
 */
@Slf4j
@RestController
@CrossOrigin
@RequestMapping(value = "/most")
public class MostController {
    private final Org2Runner org2Runner;

    private final EnvRunner envRunner;

 //   private  FabricManagerMost fabricManagerMost;
    MostController( Org2Runner org2Runner, EnvRunner envRunner){
        this.org2Runner = org2Runner;
        this.envRunner = envRunner;
//        this.fabricManagerMost = new FabricManagerMost(
//                org2Runner.om.getIntermediateOrg(org2Runner.getCc()),
//                envRunner.om.getIntermediateOrg(envRunner.getCc()));
    }

    @PostMapping("/install")
    public Object install(
            @RequestParam("chaincodeName") String chaincodeName,
            @RequestParam("chaincodeVersion") String chaincodeVersion,
            @RequestParam("chaincodeLabel") String chaincodeLabel,
            @RequestParam("chaincodeSourceLocation") String chaincodeSourceLocation,
            @RequestParam("chaincodePath") String chaincodePath,
            @RequestParam("metadadataSource") String metadadataSource,
            @RequestParam("chaincodeEndorsementPolicyPath") String chaincodeEndorsementPolicyPath) {
        // String[] args = new String[]{"CAR"+number};
        // String  chaincodeLabel = "fabcar010701_1";
        //  String  chaincodeSourceLocation = "/home/ct/go";
        //  String  chaincodePath = "github.com/hyperledger/fabric-samples/chaincode/fabcar/go";
        //  String   metadadataSource = "/home/ct/IdeaProjects/fabric-sdk-java/src/test/fixture/meta-infs/end2endit";
        //   String chaincodeEndorsementPolicyPath = "/home/ct/IdeaProjects/fabric-sdk-java/src/test/fixture/sdkintegration/chaincodeendorsementpolicy.yaml";
       try {
        IntermediateOrg org1 =  org2Runner.fm.getOrg();
        IntermediateOrg org2 =  envRunner.fm.getOrg();
        FabricManagerMost fabricManagerMost = new FabricManagerMost(
                org1,
                org2);
          return     fabricManagerMost.lifecycleChaincodeInstall(chaincodeName,chaincodeVersion,chaincodeLabel,chaincodeSourceLocation,chaincodePath,metadadataSource,chaincodeEndorsementPolicyPath);

        }catch (Exception e){
            System.out.println(e.getMessage());
            return  e.getMessage();
        }
    }


    @PostMapping("/approveformyorg")
    public Object approveformyorg(  @RequestParam("chaincodeName") String chaincodeName,
                                    @RequestParam("chaincodeVersion") String chaincodeVersion,
                                    @RequestParam("packageid") String packageid){
        try {
            IntermediateOrg org1 =  org2Runner.fm.getOrg();
            IntermediateOrg org2 =  envRunner.fm.getOrg();
            FabricManagerMost fabricManagerMost = new FabricManagerMost(
                    org1,
                    org2);

            return fabricManagerMost.lifecycleChaincodeApproveformyorg(chaincodeName,chaincodeVersion,packageid);

        }catch (Exception e){
            System.out.println(e.getMessage());
            return  e.getMessage();
        }


    }


    @PostMapping("/commit")
    public Object commit(  @RequestParam("chaincodeName") String chaincodeName,
                           @RequestParam("chaincodeVersion") String chaincodeVersion,
                           @RequestParam("packageid") String packageid){
        try {
            IntermediateOrg org1 =  org2Runner.fm.getOrg();
            IntermediateOrg org2 =  envRunner.fm.getOrg();
            FabricManagerMost fabricManagerMost = new FabricManagerMost(
                    org1,
                    org2);
            return fabricManagerMost.lifecycleChaincodeCommit(chaincodeName,chaincodeVersion,packageid);

        }catch (Exception e){
            System.out.println(e.getMessage());
            return  e.getMessage();
        }
    }

    @PostMapping("/init")
    public Object init(  @RequestParam("chaincodeName") String chaincodeName,
                           @RequestParam("chaincodeVersion") String chaincodeVersion,
                           @RequestParam("packageid") String packageid){
        try {
            IntermediateOrg org1 =  org2Runner.fm.getOrg();
            IntermediateOrg org2 =  envRunner.fm.getOrg();
            FabricManagerMost fabricManagerMost = new FabricManagerMost(
                    org1,
                    org2);
            return fabricManagerMost.lifecycleChaincodeInit(chaincodeName,chaincodeVersion,packageid);

        }catch (Exception e){
            System.out.println(e.getMessage());
            return  e.getMessage();
        }
    }
    @PostMapping("/addChaincode")
    public Object addChaincode(  @RequestParam("chaincodeName") String chaincodeName){
        IntermediateOrg org1 =  org2Runner.fm.getOrg();
        IntermediateOrg org2 =  envRunner.fm.getOrg();
        FabricManagerMost fabricManagerMost = new FabricManagerMost(
                org1,
                org2);
        fabricManagerMost.addChainCode(chaincodeName);
        return "lucia!";
    }
}
